import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from sklearn.decomposition import PCA
import word2vec

model = word2vec.load('corpusWord2Vec.bin')
# 转化为词向量
rawWordVec = model.vectors
# print(rawWordVec)
#
# print("--"*8)
# 降维
X_reduced = PCA(n_components=2).fit_transform(rawWordVec)
# print(X_reduced)

index1,metrics1 = model.cosine(u'世界')
index2,metrics2 = model.cosine(u'西藏')
index3,metrics3 = model.cosine(u'浙江')
index4,metrics4 = model.cosine(u'韩国')
index5,metrics5 = model.cosine(u'中国')

index01 = np.where(model.vocab==u'世界')
index02 = np.where(model.vocab==u'西藏')
index03 = np.where(model.vocab==u'浙江')
index04 = np.where(model.vocab==u'韩国')
index05 = np.where(model.vocab==u'中国')

index1 = np.append(index1,index01)
index2 = np.append(index2,index02)
index3 = np.append(index3,index03)
index4 = np.append(index4,index04)
index5 = np.append(index5,index05)

# 用坐标轴展示
# 中文显示需特殊处理
zhfont = matplotlib.font_manager.FontProperties(fname='C:\Windows\Fonts\STHUPO.TTF')
fig = plt.figure()
ax = fig.add_subplot(111)

for i in index1:
    ax.text(X_reduced[i][0], X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='r')
for i in index2:
    ax.text(X_reduced[i][0], X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='b')
for i in index3:
    ax.text(X_reduced[i][0], X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='g')
for i in index4:
    ax.text(X_reduced[i][0], X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='k')
for i in index5:
    ax.text(X_reduced[i][0], X_reduced[i][1], model.vocab[i], fontproperties=zhfont, color='c')

ax.set_ylim(-0.1,0.1)
ax.set_yticks(np.arange(-0.1,0.1,0.01))
ax.set_xlim(-0.1,0.1)
ax.set_xticks(np.arange(-0.1,0.1,0.01))
# ax.axis([-0.5,0.8,-0.5,0.5])
plt.show()